//
//  AddCargoInfoViewController.m
//  DA_V
//
//  Created by 中亿世联前台 on 15/5/7.
//  Copyright (c) 2015年 zhouhai. All rights reserved.
//

#import "AddCargoInfoViewController.h"
#import "AddCargoListTableViewCell.h"
#import "AddCargoListNumView.h"
#import "InterFace.h"
#import "UIViewAdditions.h"
#import "AddTableView.h"
#import "ShouRuView.h"
#import "DemandView.h"
#import "DateBut.h"
#import "ZiDonBuDanView.h"
#import "ZiDongBuHuoListModel.h"
#import "PMCalendar.h"
#import "GMDCircleLoader.h"

@interface AddCargoInfoViewController ()<ZiDongBuHuoListModelDelegate,PMCalendarControllerDelegate>
@property (nonatomic, strong) ZiDongBuHuoListModel *myzidonModel;//
@property (nonatomic, strong) DateBut *mydateBtn;//时间but
@property (nonatomic, strong) NSMutableArray *ziDonArr;//时间but


@property (nonatomic, copy) NSString *startTime;//开始时间
@property (nonatomic, copy) NSString *endTime;//结束时间
@property (nonatomic, strong) PMCalendarController *pmCC;
@end

@implementation AddCargoInfoViewController
- (instancetype)init{
    self = [super init];
    if (self) {
        self.ziDonArr = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setHidesBackButton:YES];
    
    self.myzidonModel = [[ZiDongBuHuoListModel alloc] init];
    self.myzidonModel.ziDongBuHuoListModelDelegate = self;
    NSString *agentId = [[NSUserDefaults standardUserDefaults] objectForKey:@"userID"];
    NSDictionary *parameterDic = @{@"AgentID":agentId,@"MasterOrderID":self.zidonModel.MasterOrderID};
    [self.myzidonModel getZiDongList:self.ziDonArr parameter:parameterDic];
//    [self createDateButton];
    [self createNavSubviews];
    [self createTableView];
    
    [GMDCircleLoader setOnView:self.view withTitle:@"加载中···" animated:YES];;
   
}

#pragma -mark 创建日期Button方法
- (void)createDateButton{
    
    
    self.mydateBtn = [DateBut dateButWithButton];
    
    [self.mydateBtn addTarget:self action:@selector(dateBtnActionclick:) forControlEvents:UIControlEventTouchUpInside];
    
    //    UIBarButtonItem *dateButItem = [[UIBarButtonItem alloc] initWithCustomView:self.mydateBtn2];
    //    self.navigationItem.rightBarButtonItem = dateButItem;
    
    UIWindow *windows = [[[UIApplication sharedApplication] delegate ] window];
    self.mydateBtn.frame = CGRectMake(SCREEN_WIDTH-70, 30, 60, 25);
    [windows addSubview:self.mydateBtn];
    
}
#pragma -mark 获取日期
- (void)dateBtnActionclick:(UIButton *)sender{
    NSLog(@"日期");
    self.pmCC = [[PMCalendarController alloc] init];
    self.pmCC.delegate = self;
    self.pmCC.mondayFirstDayOfWeek = YES;
    
    [self.pmCC presentCalendarFromView:sender permittedArrowDirections:PMCalendarArrowDirectionAny animated:YES];
    
    [self calendarController:self.pmCC didChangePeriod:self.pmCC.period];
    
}


static int i = 0;
- (void)calendarController:(PMCalendarController *)calendarController didChangePeriod:(PMPeriod *)newPeriod
{
    NSString *time =[NSString stringWithFormat:@"%@"
                     , [newPeriod.startDate dateStringWithFormat:@"yyyy-MM"]];
    [self.mydateBtn setTitle:time forState:UIControlStateNormal];
    
    self.startTime = [NSString stringWithFormat:@"%@%@", [newPeriod.startDate dateStringWithFormat:@"yyyy-MM-"],@"1"];
    self.endTime = [NSString stringWithFormat:@"%@", [newPeriod.startDate dateStringWithFormat:@"yyyy-MM-dd"]];
    NSLog(@"开始时间 ＝＝＝ %@,结束时间 ＝＝ %@",_startTime,_endTime);
    
    if (i%2!=0) {
        [self.pmCC dismissCalendarAnimated:YES];
        
    }
    
    i++;
    
    
}


- (BOOL)calendarControllerShouldDismissCalendar:(PMCalendarController *)calendarController{
    
    
    return YES;
}

- (void)calendarControllerDidDismissCalendar:(PMCalendarController *)calendarController{
    
    //选择日期结束
    
}
#pragma -mark 网络请求完代理
- (void)getZiDongBuHuoInfoListEnd{
    [GMDCircleLoader hideFromView:self.view animated:YES];
    NSLog(@"自动补货info ＝＝＝＝＝ %@",self.ziDonArr);
    [self createTableViewHead];
     [self tableViewFooView];
    [self.tableView reloadData];
}
#pragma -mark 创建TabView

- (void)createTableView{
    UIView *aview = [[UIView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 40)];
    aview.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:aview];
    
    
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(10, 16, 10, 10)];
    image.image = [UIImage imageNamed:@"自动补货订单---订单详情_03"];
    [aview addSubview:image];
    
    //库存订单View
    UILabel *kuCunTitle = [[UILabel alloc] initWithFrame:CGRectMake(image.right+5, 5, 200, 30)];
    kuCunTitle.text = @"自动补货订单-订单详情";
    [aview addSubview:kuCunTitle];
    
    UILabel*line = [[UILabel alloc] initWithFrame:CGRectMake(0, aview.bottom+1, SCREEN_WIDTH, 0.5)];
    line.backgroundColor = DaoHangColor;
    [self.view addSubview:line];
    
    UILabel*lineview = [[UILabel alloc] initWithFrame:CGRectMake(0, line.bottom, SCREEN_WIDTH, 10)];
    lineview.backgroundColor = LineColor;
    [self.view addSubview:lineview];
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, lineview.bottom, SCREEN_WIDTH, SCREEN_HEIGHT-lineview.frame.size.height)];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    //返回按钮
    
    UIButton *backBut = [[UIButton alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-40, SCREEN_WIDTH, 40)];

    backBut.layer.borderColor = BackColor.CGColor;
    backBut.layer.borderWidth = 1.0;
    backBut.backgroundColor = LineColor;
    [backBut addTarget:self action:@selector(BackBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [backBut setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [backBut setTitle:@"返回" forState:UIControlStateNormal];
    [self.view addSubview:backBut];
    
}

- (void)createTableViewHead{
//    @property (nonatomic,copy) NSString *MasterOrderID;           //订单编号
//    @property (nonatomic,copy) NSString *ThumbPath;               //产品缩略图地址
//    @property (nonatomic,copy) NSString *ProductID;               //产品ID
//    @property (nonatomic,copy) NSString *ProductName;             //产品名称
//    @property (nonatomic,copy) NSString *DiscountAmount;          //折扣价
//    @property (nonatomic,copy) NSString *TotalAmount;             //总金额
//    @property (nonatomic,copy) NSString *OriginStock;             //原库存
//    @property (nonatomic,copy) NSString *AddTime;                 //下单时间
//    
//    @property (nonatomic,copy) NSString *Profit;                  //盈利
//    
//    @property (nonatomic,copy) NSString *MakeupStock;             //库存补充
//    @property (nonatomic,copy) NSString *SaleStock;               //发给下级
//    @property (nonatomic,copy) NSString *SubAddTime;               //发给下级
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 110)];
    
    //订单号
    ZiDonBuDanView *addCargolistView = [[[NSBundle mainBundle] loadNibNamed:@"Shopping_list_xibs" owner:self options:nil] objectAtIndex:6];
    addCargolistView.frame = CGRectMake(10, 5, SCREEN_WIDTH-20, 40);
//    @property (weak, nonatomic) IBOutlet UILabel *listNium;//订单号
//    
//    @property (weak, nonatomic) IBOutlet UILabel *oldPrice;//打折后价格
//    
//    @property (weak, nonatomic) IBOutlet UILabel *nowPrice;//未打折价格
//    
//    @property (weak, nonatomic) IBOutlet UILabel *xiaDanTime;//下单时间
//    
//    
//    @property (weak, nonatomic) IBOutlet UILabel *profitLabel;//利润
//    
//    @property (weak, nonatomic) IBOutlet UILabel *profitNumber;//利润金额
    if (self.myzidonModel.MasterOrderID.length) {
        addCargolistView.listNium.text   = [NSString stringWithFormat:@"订单号:%@",self.myzidonModel.MasterOrderID];
    }else{
        addCargolistView.listNium.text   = @"0";
    }
    if (self.myzidonModel.AddTime.length) {
        addCargolistView.xiaDanTime.text = [NSString stringWithFormat:@"下单时间:%@",self.myzidonModel.AddTime];
    }else{
        addCargolistView.xiaDanTime.text = @"0";
    }
    if (self.myzidonModel.DiscountAmount.length) {
        addCargolistView.oldPrice.text   = [NSString stringWithFormat:@"¥%d",self.myzidonModel.DiscountAmount.intValue];
    }else{
        addCargolistView.oldPrice.text   = @"¥0";
    }
    if (self.myzidonModel.TotalAmount.length) {
        addCargolistView.nowPrice.text   = [NSString stringWithFormat:@"(¥%d)",self.myzidonModel.TotalAmount.intValue];
    }else{
        addCargolistView.nowPrice.text   = @"¥0";
    }
    addCargolistView.profitLabel.hidden = YES;
    addCargolistView.profitNumber.hidden = YES;
    [headView addSubview:addCargolistView];
    

    //库存变化
    UILabel *changetitle = [[UILabel alloc] initWithFrame:CGRectMake(10, addCargolistView.bottom+5, 100, 21)];
    changetitle.text     = @"库存变化";
    changetitle.font     = [UIFont fontWithName:nil size:15];
    [headView addSubview:changetitle];

    //表格标题
    AddTableView *tablevIew           = [[[NSBundle mainBundle] loadNibNamed:@"Shopping_list_xibs" owner:self options:nil] objectAtIndex:7];
    tablevIew.frame                   = CGRectMake(10, changetitle.bottom+5, SCREEN_WIDTH - 20, 40);
    tablevIew.shangPingName.textColor = [UIColor colorWithRed:101/255.0 green:101/255.0 blue:101/255.0 alpha:1];
    tablevIew.kuCunNum.textColor      = [UIColor colorWithRed:101/255.0 green:101/255.0 blue:101/255.0 alpha:1];
    tablevIew.addCunCunNum.textColor  = [UIColor colorWithRed:101/255.0 green:101/255.0 blue:101/255.0 alpha:1];
    tablevIew.giveNext.textColor      = [UIColor colorWithRed:101/255.0 green:101/255.0 blue:101/255.0 alpha:1];
    [headView addSubview:tablevIew];
    
    self.tableView.tableHeaderView = headView;
    
}

#pragma -mark 创建导航返回按钮
- (void)createNavSubviews{
    UIView *qview     = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    UIButton *backBut = [UIButton buttonWithType:UIButtonTypeCustom];
    backBut.frame     = CGRectMake(-25, -2, 85, 44);
    [backBut setImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
    [backBut addTarget:self action:@selector(BackBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [qview addSubview:backBut];
    
    UILabel *title      = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-110, 5, 200, 30)];
    title.textAlignment = NSTextAlignmentCenter;
    title.font          = [UIFont fontWithName:@"Helvetica-Bold" size:17];
    title.text          = @"自动补货订单-订单详情";
    title.textColor     = [UIColor whiteColor];
    [qview addSubview:title];
    self.navigationItem.titleView = qview;
}
#pragma -mark 返回按钮事件
- (void)BackBtnClick{
    [self.mydateBtn removeFromSuperview];
    [self.navigationController popViewControllerAnimated:YES];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.ziDonArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

#pragma -mark 创建TableView的FoodView
- (void )tableViewFooView{
//    @property (nonatomic,copy) NSString *Profit;                  //盈利
//    
//    @property (nonatomic,copy) NSString *MakeupStock;             //库存补充
//    @property (nonatomic,copy) NSString *SaleStock;               //发给下级
//    @property (nonatomic,copy) NSString *SubAddTime;               //库存来源的下单时间
//    
//    
//    

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-20, 140)];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, (SCREEN_WIDTH-20), 1)];
    lineView.backgroundColor = LineColor;
    [view addSubview:lineView];
    
//    @property (weak, nonatomic) IBOutlet UILabel *listNumber;//订单号
//    
//    @property (weak, nonatomic) IBOutlet UILabel *nextName;//库存需求来自的用户名
//    
//    @property (weak, nonatomic) IBOutlet UILabel *xiaDanTime;//下单时间
//    
//    @property (weak, nonatomic) IBOutlet UILabel *nowPrice;//打折总金额
//    
//    @property (weak, nonatomic) IBOutlet UILabel *oldPrice;//未打折总金额
//    addCargolistView.listNium.text = [NSString stringWithFormat:@"订单号:%@",self.myzidonModel.MasterOrderID];
//    addCargolistView.xiaDanTime.text = [NSString stringWithFormat:@"下单时间:%@",self.myzidonModel.AddTime];
//    addCargolistView.oldPrice.text = [NSString stringWithFormat:@"¥%d",self.myzidonModel.DiscountAmount.intValue];
//    addCargolistView.nowPrice.text = [NSString stringWithFormat:@"(¥%d)",self.myzidonModel.TotalAmount.intValue];
    
    DemandView *demandView = [[[NSBundle mainBundle] loadNibNamed:@"Shopping_list_xibs" owner:self options:nil] objectAtIndex:8];
     demandView.nextName.text = [NSString stringWithFormat:@"库存需求来自:%@",self.myzidonModel.SubCustomerName];
     demandView.listNumber.text = [NSString stringWithFormat:@"下线订单:%@",self.myzidonModel.SubMasterOrderID];
     demandView.xiaDanTime.text =[NSString stringWithFormat:@"%@",self.myzidonModel.SubAddTime];
     demandView.nowPrice.text = [NSString stringWithFormat:@"¥%.f",self.myzidonModel.SubDiscountAmount.floatValue];
    demandView.oldPrice.text = [NSString stringWithFormat:@"(¥%.f)",self.myzidonModel.SubTotalAmount.floatValue];
//    if (self.myzidonModel.SubMasterOrderID.length) {
//        demandView.listNumber.text = [NSString stringWithFormat:@"下线订单:%@",self.myzidonModel.SubMasterOrderID];//self.myzidonModel.SubMasterOrderID;
//    }else{
//        demandView.listNumber.text = @"0";
//    }
//    if (self.myzidonModel.SubAddTime.length) {
//        demandView.xiaDanTime.text =[NSString stringWithFormat:@"下单时间:%@",self.myzidonModel.SubAddTime];
//    }else{
//        demandView.xiaDanTime.text = @"0";
//    }
//    if (self.myzidonModel.SubCustomerName.length) {
//        demandView.nextName.text = [NSString stringWithFormat:@"库存需求来自:%@",self.myzidonModel.SubCustomerName];//self.myzidonModel.SubCustomerName;
//    }else{
//        demandView.nextName.text = @"-";
//    }
//    if (self.myzidonModel.SubDiscountAmount.length) {
//        demandView.nowPrice.text = [NSString stringWithFormat:@"¥%.f",self.myzidonModel.SubDiscountAmount.floatValue];//self.myzidonModel.SubDiscountAmount;
//    }else{
//        demandView.nowPrice.text = @"¥0";
//    }
//    if (self.myzidonModel.SubTotalAmount.length) {
//        demandView.oldPrice.text = [NSString stringWithFormat:@"(¥%.f)",self.myzidonModel.SubTotalAmount.floatValue];//self.myzidonModel.SubTotalAmount;
//    }else{
//        demandView.oldPrice.text = @"¥0";
//    }
    
    demandView.frame = CGRectMake(10, lineView.bottom+5, SCREEN_WIDTH - 20, 48);
    [view addSubview:demandView];
    
    
//    @property (weak, nonatomic) IBOutlet UILabel *fromNextPrice;//来自下线的进货款
//    
//    @property (weak, nonatomic) IBOutlet UILabel *zhiChuPrice;//支出进货款
//    
//    @property (weak, nonatomic) IBOutlet UILabel *zhiChuOldPrice;//支出原库存金额
//    
//    
//    @property (weak, nonatomic) IBOutlet UILabel *profitLabel;//利润
    
    
    //    @property (nonatomic,copy) NSString *SubMasterOrderID;           //库存来源的订单编号
    //    @property (nonatomic,copy) NSString *SubCustomerName;            //来源订单的订货人姓名
    //    @property (nonatomic,copy) NSString *SubTotalAmount;             //来源订单的总金额
    //    @property (nonatomic,copy) NSString *SubDiscountAmount;          //来源订单的折扣价
    //    @property (nonatomic,copy) NSString *Income;                     //收到来自下线进货款
    //    @property (nonatomic,copy) NSString *Outcome;                    //支出进货款
    ShouRuView *shouRuView  = [[[NSBundle mainBundle] loadNibNamed:@"Shopping_list_xibs" owner:self options:nil] objectAtIndex:9];
    shouRuView.frame = CGRectMake(10, demandView.bottom+5, SCREEN_WIDTH - 20, 90);
    
    shouRuView.fromNextPrice.text = [NSString stringWithFormat:@"¥%.1f",self.myzidonModel.Income.floatValue];
      shouRuView.zhiChuPrice.text = [NSString stringWithFormat:@"¥%.1f",self.myzidonModel.Outcome.floatValue];
     shouRuView.zhiChuOldPrice.text = [NSString stringWithFormat:@"¥%.1f",self.myzidonModel.OutcomeStock.floatValue];
    shouRuView.profitLabel.text = [NSString stringWithFormat:@"¥%1.f",self.myzidonModel.Profit.floatValue];
//    if (self.myzidonModel.Income.length) {
//        shouRuView.fromNextPrice.text = [NSString stringWithFormat:@"¥%.1f",self.myzidonModel.Income.floatValue];
//    }else{
//        shouRuView.fromNextPrice.text = @"¥0";
//    }
//    if (self.myzidonModel.Outcome.length) {
//        shouRuView.zhiChuPrice.text = [NSString stringWithFormat:@"¥%.1f",self.myzidonModel.Outcome.floatValue];
//    }else{
//        shouRuView.zhiChuPrice.text = @"¥0";
//    }
//    if (self.myzidonModel.OutcomeStock.floatValue > 0) {
//        shouRuView.zhiChuOldPrice.text = [NSString stringWithFormat:@"¥%.1f",self.myzidonModel.OutcomeStock.floatValue];
//    }else{
//        shouRuView.zhiChuOldPrice.text = @"¥0";
//    }
//    if (self.myzidonModel.Profit.floatValue > 0) {
//        shouRuView.profitLabel.text = [NSString stringWithFormat:@"¥%1.f",self.myzidonModel.Profit.floatValue];
//    }else{
//        shouRuView.profitLabel.text = @"¥0";
//    }
    [view addSubview:shouRuView];
    self.tableView.tableFooterView = view;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *iden = @"shoppingCell";
    AddCargoListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
    if (cell == nil) {
        cell  = [[AddCargoListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
        
    }
    ZiDongBuHuoListModel *model = self.ziDonArr[indexPath.row];
    [cell showListView:model];
    return cell;
}
@end
